//
//  LLStatusListViewModel.swift
//  新浪微博
//
//  Created by mac on 16/5/28.
//  Copyright © 2016年 mac.com. All rights reserved.
//

import UIKit

class LLStatusListViewModel: NSObject {
    lazy var statusViewModelArray: [LLStatusViewModel] = [LLStatusViewModel]()
    func loadData(isPulling: Bool,finished: (isSusscess: Bool) -> () ) {
        //数组的第一个元素的status.id
        //根据是否正在转动处理 since_id 和 max_id 为互斥参数
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
        if isPulling {
            max_id = statusViewModelArray.last?.status?.id ?? 0
        } else {
            since_id = statusViewModelArray.first?.status?.id ?? 0
        }
LLNetworkTools.shardTools.loadHomePageData(since_id, max_id: max_id) { (responseObject, error) -> () in
    if error != nil {
        finished(isSusscess: false)
        print("-----------")
        print(error)
        print("++++++++++++")
        return
    }
    guard let dict = responseObject as? [String : AnyObject] else
    {
        finished(isSusscess: false)
        return
    }
    guard let arr = dict["statuses"] as? [[String : AnyObject]] else
    {
        finished(isSusscess: false)
        return
    }
    var arrTemp = [LLStatusViewModel]()
    for item in arr{
        let s = LLStatus(dict: item)
        let viewModel = LLStatusViewModel(status: s)
        arrTemp.append(viewModel)
        
    }
    
    if isPulling{
        //加载更多数据
        self.statusViewModelArray = self.statusViewModelArray + arrTemp
        
    }else{
        //下啦刷新
        self.statusViewModelArray = arrTemp + self.statusViewModelArray
    }
    finished(isSusscess: true)
    
        }
    }

        }
    